const express = require('express');
const router = express.Router();
const opera = require('../utils/dbOperation')
const result = require('../utils/response')
const uid = require('../utils/getUid')
const time = require('../utils/timeUtils')

router.delete('/delete',(req,res) =>{
  const data = req.body;
  opera.update('pro_fun', { 'del_falg': 1 }, data, (results) => {
    res.send(result.success())
  })
})

router.post('/list',(req,res) =>{
  const data = req.body;
  data.del_falg = 0;
  opera.select('pro_fun', data, (results) => {
    res.send(result.success(results))
  },null,'create_time')
})


router.post('/add',(req,res) =>{
  const data = req.body;
  data.id = uid();

  data.update_time = time.getNowFormatDate();
  data.create_time = time.getNowFormatDate();
  console.log(data);
  if (data.module_id && data.module_id == '0') {
    delete data.module_id;
  }

  opera.insert('pro_fun',data,(results) => {
    res.send(result.success(data))
  })
})

router.post('/detail',(req,res) => {
  const data = req.body;
  opera.findOne('pro_fun',data,(back) =>{
    opera.select('pro_fun_arg',{fun_id:data.id,pid:0},(argBack) => {
      const data = back;
      data.arg = argBack;
      res.send(result.success(data))
    })

  })
})

router.put('/update',(req,res) => {
  const data = req.body;
  const id = data.id;
  delete data.id;
  data.update_time = time.getNowFormatDate();
  opera.update('pro_fun',data,{id:id},(back) => {
    res.send(result.success())
  })
})

module.exports = router;
